import 'dart:async';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/common/action/index.dart';
import 'package:qgchat/http/apis/user.dart';
import 'package:qgchat/http/models/blackListModel.dart';

class BlackListBloc implements BlocBase {
  // ##########  STREAMS  ##############

  /// 黑名单列表
  StreamController _black = StreamController<List<BlackListModel>>.broadcast();
  Sink get _blackSink => _black.sink;
  Stream get blackStream => _black.stream;

  // ##########  HANDLING  ##############

  BlackListBloc() {
    getBlack();
  }

  void dispose() {
    _black.close();
  }

  List<BlackListModel> blacklist = [];

  /// 黑名单列表
  void getBlack() async {
    List<BlackListModel> result = await UserApis.blackList();
    blacklist = result;
    _blackSink.add(result);
  }

  /// 拉白某条粉肠
  void outBlack(int id) async {
    int i = blacklist.indexWhere((item) => item.id == id);
    Map result = await CommonAction.outBlack(id);
    if (result['status'] == 1 && i != -1) {
      blacklist.removeAt(i);
    }
  }
}
